package com.sina.shop.manage.controller.goods;

import com.sina.shop.core.exception.server.UpdateException;
import com.sina.shop.domain.goods.Goods;
import com.sina.shop.manage.service.goods.GoodsManageService;
import com.sina.shop.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * 段浩杰   2018-01-11-16:29
 */
@Controller
@RequestMapping("goodsManage")
public class GoodsManageController extends BaseController {

    @Autowired
    @Qualifier("goodsManageService")
    private GoodsManageService goodsManageService;

    @RequestMapping(value = "getGoodsList", method = RequestMethod.GET)
    public ModelAndView getGoodsList(@Valid Goods goods) {
        LOG.info("goods:{}", goods);

        ModelAndView mv = new ModelAndView("resultView");
        Map<String, Object> result = new HashMap<>(6);
        Long count[] = new Long[1];
        List<Map<String, Object>> permissionList = goodsManageService.getGoodsList(goods, count);

        Map<String, Object> data = new HashMap<>(4);
        data.put("recordsTotal", count[0]);
        data.put("recordsFiltered", count[0]);
        data.put("data", permissionList);

        result.put(CODE, OK);
        result.put(DATA, data);
        mv.addObject(RESULT, result);

        return mv;
    }

    @RequestMapping(value = "getGoodsIdAndName", method = RequestMethod.GET)
    public ModelAndView getGoodsIdAndName() {

        ModelAndView mv = new ModelAndView("resultView");
        Map<String, Object> result = new HashMap<>(6);

        List<Map<String, Object>> goodsList = goodsManageService.getGoodsIdAndName();

        Map<String, Object> data = new HashMap<>(4);
        data.put("data", goodsList);

        result.put(CODE, OK);
        result.put(DATA, data);
        mv.addObject(RESULT, result);

        return mv;
    }

    @RequestMapping(value = "deleteGoods", method = RequestMethod.POST)
    public ModelAndView deleteGoods(Long id) {
        LOG.info("id:{}", id);

        ModelAndView mv = new ModelAndView("resultView");
        Map<String, Object> result = new HashMap<>(4);

        try {
            goodsManageService.deleteGoods(id);
            result.put(CODE, OK);
            result.put(MSG, "删除商品成功");
        } catch (UpdateException e) {
            result.put(CODE, e.getResultCode());
            result.put(MSG, e.getMessage());
        }

        mv.addObject(RESULT, result);

        return mv;
    }


    @RequestMapping(value = "updateGoods", method = RequestMethod.POST)
    public ModelAndView updateGoods(@Valid Goods goods) {

        LOG.info("goods:{}", goods);

        ModelAndView mv = new ModelAndView("resultView");
        Map<String, Object> result = new HashMap<>(4);
        try {
            goodsManageService.updateGoods(goods);
            result.put(CODE, OK);
            result.put(MSG, "更新商品成功");
        } catch (UpdateException e) {
            result.put(CODE, e.getResultCode());
            result.put(MSG, e.getMessage());
        }

        mv.addObject(RESULT, result);

        return mv;
    }

    @RequestMapping(value = "saveGoods", method = RequestMethod.POST)
    public ModelAndView saveGoods(@Valid Goods goods) {

        LOG.info("goods:{}", goods);


        ModelAndView mv = new ModelAndView("resultView");
        Map<String, Object> result = new HashMap<>(4);
        try {
            goodsManageService.saveGoods(goods);
            result.put(CODE, OK);
            result.put(MSG, "新增商品成功");
        } catch (UpdateException e) {
            result.put(CODE, e.getResultCode());
            result.put(MSG, e.getMessage());
        }

        mv.addObject(RESULT, result);

        return mv;
    }


    @RequestMapping(value = "getGoodsListToHtml", method = RequestMethod.GET)
    private ModelAndView getGoodsListToHtml() {

        ModelAndView mv = new ModelAndView("goods/goodsList");

        return mv;
    }
}
